package com.kgc.springboot.hangduo.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.springboot.hangduo.pojo.Catalog;
import com.kgc.springboot.hangduo.pojo.Clause;
import com.kgc.springboot.hangduo.pojo.Laws;
import com.kgc.springboot.hangduo.service.CatalogService;
import com.kgc.springboot.hangduo.service.ClauseService;
import com.kgc.springboot.hangduo.service.LawsService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ASUS
 * @date: 2020/12/27 14:22
 * @description:
 */
@Controller
public class ClauseController {

    @Resource
    ClauseService clauseService;

    @Resource
    LawsService lawsService;

    @Resource
    CatalogService catalogService;

    @RequestMapping("showModifyPage")
    public String showModifyPage(ModelMap modelMap
            , Clause clause){

        List<Laws> laws = lawsService.selectAll();
        List<Catalog> catalogs = catalogService.searchAll();

        modelMap.addAttribute("laws",laws).addAttribute("catalogs",catalogs).addAttribute("putUrl","insertClause");;
        if (clause.getId() != null){
            modelMap.addAttribute("clause",clause).addAttribute("putUrl","updateClauseSelective");
            return "terms-edit";
        }

        return "terms-edit";
    }

    @RequestMapping("searchByCondition")
    public String searchByCondition(ModelMap modelMap
            ,Integer lawId
            , String clauseNum
            , String keyWord
            , @RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "8")Integer pageSize){
        List<Laws> laws = lawsService.selectAll();
        modelMap.addAttribute("laws",laws);
        PageInfo<Clause> pageInfo = clauseService.searchByCondition(lawId, clauseNum, keyWord, pageNum, pageSize);
        modelMap.addAttribute("pageInfo",pageInfo).addAttribute("lawId",lawId).addAttribute("keyWord",keyWord).addAttribute("clauseNum",clauseNum);
        return "terms-list";
    }

    @ResponseBody
    @RequestMapping("searchByConditionToPage")
    public Map<String,Object> searchByConditionToPage(Integer lawId
            , String clauseNum
            , String keyWord
            , @RequestParam(defaultValue = "1") Integer pageNum
            , @RequestParam(defaultValue = "8")Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("info","请求错误！");
        map.put("status",-1);
        map.put("lawId",lawId);
        map.put("clauseNum",clauseNum);
        map.put("keyWord",keyWord);

        PageInfo<Clause> pageInfo = clauseService.searchByCondition(lawId, clauseNum, keyWord, pageNum, pageSize);
        if (pageInfo.getList()!=null && pageInfo.getList().size()>0){
            map.put("pageInfo",pageInfo);
            map.put("info","成功！");
            map.put("status",1);
        }else{
            map.put("info","失败！");
            map.put("status",0);
        }

        return map;
    }

    @ResponseBody
    @RequestMapping("insertClause")
    public Map<String,Object> insertClause(Clause clause){
        Map<String,Object> map = new HashMap<>();
        Integer res = clauseService.insertClause(clause);

        if (res>0){
            map.put("info","成功！");
            map.put("status",1);
        }else if (res == 0){
            map.put("info","失败！");
            map.put("status",0);
        }else {
            map.put("info","请求错误！");
            map.put("status",-1);
        }

        return map;
    }

    @ResponseBody
    @RequestMapping("updateClauseSelective")
    public Map<String,Object> updateClauseSelective(Clause clause){
        Map<String,Object> map = new HashMap<>();
        Integer res = clauseService.updateClauseSelective(clause);
        if (res>0){
            map.put("info","成功！");
            map.put("status",1);
        }else if (res == 0){
            map.put("info","失败！");
            map.put("status",0);
        }else {
            map.put("info","请求错误！");
            map.put("status",-1);
        }

        return map;
    }

    @ResponseBody
    @RequestMapping("delClauseById")
    public Map<String,Object> delClauseById(Integer id){
        Map<String,Object> map = new HashMap<>();

        Integer res = clauseService.delClauseById(id);
        if (res>0){
            map.put("info","成功！");
            map.put("status",1);
        }else if (res == 0){
            map.put("info","失败！");
            map.put("status",0);
        }else {
            map.put("info","请求错误！");
            map.put("status",-1);
        }

        return map;



    }

    @RequestMapping("/showClause")
    public String showClause(ModelMap modelMap
            , Integer id,@RequestParam(value = "catalog",required = false) String catalog,
                             @RequestParam(value = "catalogId",required = false)Integer catalogId){
        Clause clause = clauseService.selectById(id);
        if(catalog==null){
            Catalog catalog1 = catalogService.selectById(catalogId);
            catalog=catalog1.getCatalog();
        }
        modelMap.addAttribute("clause",clause);
        modelMap.addAttribute("catalog",catalog);
        return "user-clause";
    }

    @RequestMapping("/selectClauseTitleByCondition")
    public String selectClauseByCondition(ModelMap modelMap,String condition){
        List<Clause> clauses = clauseService.selectByCondition(condition);
        modelMap.addAttribute("clauses",clauses).addAttribute("condition",condition);
        return "user-clause-list";
    }
}
